package servlet;

import dao.DaoFactory;
import entity.Global;
import entity.Student;
import entity.Teacher;
import utils.MD5;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

import java.sql.SQLException;

@WebServlet(name="login",urlPatterns={"/login"})
public class LoginServlet extends HttpServlet {


    private static final long serialVersionUID = 938013082140284103L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String type = req.getParameter("type");
        HttpSession session = req.getSession();
        session.setAttribute("type",type);
        try {
            Global global = DaoFactory.getInstance().getGlobalDao().getGlobal();
            session.setAttribute("global", global);
            if("0".equals(type)){
                Student student = DaoFactory.getInstance().getStudentDao().login(username,password);
                if(student!=null){
                    session.setAttribute("user",student);
                    resp.setContentType("text/html;charset=utf-8");
                    PrintWriter out = resp.getWriter();
                    out.print("page/Student/index.jsp");
                }else{
                    resp.setContentType("text/html;charset=utf-8");
                    PrintWriter out = resp.getWriter();
                    out.print("用户名或密码错误");
                }
            }else if("2".equals(type)){//有管理员权限的老师，或者管理员
                Teacher teacher = DaoFactory.getInstance().getTeacherDao().login(username, MD5.getMD5(password));
                if(teacher!=null){
                    session.setAttribute("user",teacher);
                    resp.setContentType("text/html;charset=utf-8");
                    PrintWriter out = resp.getWriter();
                    System.out.println("登录成功");
                    out.print("page/Admin/index.jsp");
                }else{
                    resp.setContentType("text/html;charset=utf-8");
                    PrintWriter out = resp.getWriter();
                    out.print("用户名或密码错误");
                }
            }else{
                Teacher teacher = DaoFactory.getInstance().getTeacherDao().login(username,MD5.getMD5(password));
                if(teacher!=null){
                    session.setAttribute("user",teacher);
                    resp.setContentType("text/html;charset=utf-8");
                    PrintWriter out = resp.getWriter();
                    out.print("page/Teacher/index.jsp");
                }else{
                    resp.setContentType("text/html;charset=utf-8");
                    PrintWriter out = resp.getWriter();
                    out.print("用户名或密码错误");
                }
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

}
